/**
 * 商品类
 */
class goods extends eui.Component {
	public constructor(game) {
		super();
		this.gameHandle = game;
	}
	private gameHandle;

	/**商品类型 */
	private disGoodsType: string[] = ["shipin", "tuowei"];
	/**点击商品动作 */
	//	private GoodsAction: string[] = ["goumai", "chuandai", "xiexia"];

	private GoodsAction: string[] = ["goumai", "equip_png", "xiexia"];
	/**用于显示商品背景 */
	public disGoodsBg: eui.Image;
	/**用于显示商品图片 */
	public disGoodsImage: eui.Image;
	/**用于显示商品名字 */
	public disGoodsName: eui.Label;
	/**用于显示商品按键 */
	public disGoodsButton: eui.Image;
	/**用于显示商品价格 */
	public disGoodsPrice: eui.Label;
	/**用于显示商品的金币 */
	public disGoodsGold: eui.Image;
	/**试用按钮 */
	public TryButton: eui.Image;
	/**试用中按钮 */
	public TryingButton: eui.Image;
	/**商品ID */
	public shopId: any;
	/**商品类型 */
	public goodsType;
	/**商品名字 */
	public goodsName;
	/**商品价格 */
	public goodsPrice;
	/**商品资源名字 */
	public resName;
	/**是否拥有 */
	public isHave = false;
	private colo = [
		[
			0.33, 0.33, 0.33, 0, 0,
			0.33, 0.33, 0.33, 0, 0,
			0.33, 0.33, 0.33, 0, 0,
			0, 0, 0, 1, 0
		], [
			1, 0, 0, 0, 0,
			0, 1, 0, 0, 0,
			0, 0, 1, 0, 0,
			0, 0, 0, 1, 0
		]
	]
	/**  商品初始化*/
	public init() {
		this.width = 163;
		this.height = 187;
		this.setShopBg();
		this.setShopImage();
		this.setShopName();
		this.setShopButton();
		this.setShopPrice();
		this.setClickButtonEvent();
		this.setTryButton();
		this.setTryingButton();
		/**控制试用按钮监听 */
		if (egret.localStorage.getItem("1") == "true") {
			this.removeEventListener(egret.Event.ADDED_TO_STAGE, this.setTryClickEvent, this);
			this.TryButton.visible = false;
		} else {
			this.addEventListener(egret.Event.ADDED_TO_STAGE, this.setTryClickEvent, this);
		}
	}

	public DisplayShops(_X: number, _Y: number) {
		this.x = _X;
		this.y = _Y;
		if (this.isHave == true) {
			this.changeShopAction(1);
		}
	}

	/**商品是否解锁 */
	private unlock(aa: any) {
		if (this.isHave) {
			var colorFlilter = new egret.ColorMatrixFilter(this.colo[1]);
		} else {
			var colorFlilter = new egret.ColorMatrixFilter(this.colo[0]);
		}
		aa.filters = [colorFlilter];

	}
	/**设置商品背景 */
	private setShopBg() {
		this.disGoodsBg = this.loadingImage("shangpinkuang", 0, 0, 166, 166);
		/**商品背景 */
		this.addChild(this.disGoodsBg);
		this.unlock(this.disGoodsBg);
	}
	/**设置商品图片 */
	private setShopImage() {
		this.disGoodsImage = this.loadingImage(this.resName, 0, 0, 100, 50);
		/**商品图片 */
		this.addChild(this.disGoodsImage);
		this.unlock(this.disGoodsImage);
	}
	/**设置商品名字 */
	private setShopName() {
		this.disGoodsName = this.loadingLabel(this.goodsName, 0, -68, 131, 35, 0, 20);
		/**商品名字 */
		this.addChild(this.disGoodsName);
		this.unlock(this.disGoodsName);
	}
	/**设置商品按钮 */
	private setShopButton() {
		this.disGoodsButton = this.loadingImage(this.GoodsAction[0], 0, 80, 120, 58);
		/**商品操作 */
		this.addChild(this.disGoodsButton);
	}
	/**设置试用按钮 */
	private setTryButton() {
		this.TryButton = this.loadingImage("Try2_png", 0, 80, 120, 58);
		this.TryButton.visible = false;
		this.TryButton.touchEnabled = true;
		this.addChild(this.TryButton);
	}
	/**设置试用中按钮 */
	private setTryingButton() {
		this.TryingButton = this.loadingImage("trying_png", 0, 80, 120, 58);
		this.TryingButton.visible = false;
		//this.TryingButton.touchEnabled = false;
		this.addChild(this.TryingButton);
	}
	/**设置商品价格 */
	private setShopPrice() {
		this.disGoodsPrice = this.loadingLabel("10000", -20, 77, 111, 33, -5, 18);
		this.disGoodsGold = this.loadingImage("golds", 30, 75, 40, 40);
		//关闭触碰
		//	this.disGoodsGold.touchEnabled = false;
		/**商品金币 */
		this.addChild(this.disGoodsGold);
		/**商品价格 */
		this.addChild(this.disGoodsPrice);
	}
	/**设置商品点击事件 */
	private setClickButtonEvent() {
		this.disGoodsPrice.touchEnabled = false;
		this.disGoodsGold.touchEnabled = false;
		this.disGoodsButton.touchEnabled = true;
		this.disGoodsButton.addEventListener(egret.TouchEvent.TOUCH_TAP, e => {
			SoundManager.getInstance().playEffectSound("click_mp3")
			egret.Tween.get(this.disGoodsButton).to({ scaleX: 0.7, scaleY: 0.7 }, 100).to({ scaleX: 1, scaleY: 1 }, 100);
			egret.Tween.get(this.disGoodsGold).to({ scaleX: 0.7, scaleY: 0.7 }, 100).to({ scaleX: 1, scaleY: 1 }, 100);
			egret.Tween.get(this.disGoodsPrice).to({ scaleX: 0.7, scaleY: 0.7 }, 100).to({ scaleX: 1, scaleY: 1 }, 100);
			if (DataProl.userCurrentDressUp.userMoney >= 10000) {
				console.log(DataProl.userCurrentDressUp.userMoney + "购买成功！");
				DataProl.userCurrentDressUp.userMoney -= 10000;
				this.gameHandle.gameHandle.operatingMerchandise(this.shopId);
				this.isHave = true;
			} else {
				console.log(DataProl.userCurrentDressUp.userMoney + "购买失败！");
				//提示蛙币不足弹框
				this.gameHandle.gameHandle.showToolTip();
			}

			// if (!this.isHave) {
			// 	var fashezhuo = new egret.Point(this.x, this.y);
			// 	//this.gameHandle.gameHandle.buyGoods(this.shopId, this.goodsType, fashezhuo);
			// 	//this.gameHandle.gameHandle.showToolTip();

			// }
		}, this);
	}
	/**试用按钮点击事件 */
	private setTryClickEvent() {
		this.TryButton.addEventListener(egret.TouchEvent.TOUCH_TAP, async e => {
			wxtool.Instance.shopnum = this.shopId;
			const isEnded = await platform.showVideo();
			wxtool.Instance.reward(isEnded);
			//wxtool.Instance.OpenAD("adunit-423aa89d6762dee9");
			//	this.TryingButton.visible = true;
			this.disGoodsGold.visible = false;
			this.disGoodsPrice.touchEnabled = false;
			this.disGoodsButton.touchEnabled = false;
			console.log(wxtool.Instance.shopnum);
		}, this);
	}

	/**改变点击商品动作显示 */
	public changeShopAction(actionIndex: number) {
		this.disGoodsPrice.text = "";
		this.disGoodsGold.visible = false;
		this.disGoodsButton.texture = RES.getRes(this.GoodsAction[actionIndex]);
	}
	/**
	* resName 资源名字
	* x/y 坐标
	* width/heigth 尺寸
	*/
	private loadingImage(resName: string, _x: number, _y: number, width: number, heigth: number): any {
		var Image: eui.Image = new eui.Image();
		Image.texture = RES.getRes(resName);
		Image.width = width;
		Image.height = heigth;
		Image.x = _x;
		Image.y = _y;
		Image.anchorOffsetX = Image.width / 2;
		Image.anchorOffsetY = Image.height / 2;
		return Image;
	}


	/**
	 * content 内容 
	 */
	private loadingLabel(content: string, _x: number, _y: number, width: number, heigth: number, rotation: number, size: number): any {
		var Label: eui.Label = new eui.Label(content);
		Label.x = _x;
		Label.y = _y;
		// Label.width = width;
		// Label.height = heigth;
		Label.rotation = rotation;
		Label.size = size;
		Label.anchorOffsetX = Label.width / 2;
		Label.anchorOffsetY = Label.height / 2;
		Label.textAlign = egret.HorizontalAlign.CENTER;
		Label.verticalAlign = egret.VerticalAlign.MIDDLE;
		Label.fontFamily = "Microsoft YaHei";
		return Label;
	}

	/**信息初始化 */
	public dataInit(goodsData) {
		this.shopId = goodsData.shopId;
		this.name = this.shopId.toString();
		this.goodsType = goodsData.shopType;
		this.goodsPrice = goodsData.shopMoney;
		this.goodsName = goodsData.shopName;
		this.resName = goodsData.shopResName;
	}
}